package com.auto.api.mapping.component;

import com.auto.api.core.entity.ApiInfo;
import com.auto.api.core.service.ApiStoreService;
import com.auto.api.core.service.ApiInfoService;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * Description: api接口项服务类
 *
 * @author liangshf
 * @date 2019-04-30
 */
@Component
public class ApiInfoManager {

    @Autowired
    private ApiInfoService apiInfoService;

    @Autowired
    private ApiStoreService apiStore;

    private List<ApiInfo> apiInfoList;

    /**
     * 容器初始化完成事件 初始化解析SpringBean mapping
     */
    public void processApiList() {
        apiInfoList = apiStore.scanApiList();
        //保存操作
        apiInfoService.saveApiItem(apiInfoList);
    }

    public List<ApiInfo> findList(ApiInfo param) {
        Stream<ApiInfo> stream = apiInfoList.stream();
        if (StringUtils.isNotEmpty(param.getMethodName())) {
            stream = stream.filter(i -> i.getMethodName().equals(param.getMethodName()));
        }
        if (StringUtils.isNotEmpty(param.getRequestUrl())) {
            stream = stream.filter(i -> i.getRequestUrl().indexOf(param.getRequestUrl()) != -1);
        }
        return stream.collect(Collectors.toList());
    }
}
